package exportPDF;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;


import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;

import utils.DateUtils;
import utils.UtilsOnScopes;

public class ExporToPDF<T> {
	public static String realpath = ""; 
	public static String pathDownload="";
    public static String fileNameDownload ="";
	public ExporToPDF(){
		
	}
	
	
	public static String getRealpath() {
		return realpath;
	}

	public static void setRealpath(String realpath) {
		ExporToPDF.realpath = realpath;
	}

	public static String getPathDownload() {
		return pathDownload;
	}

	public static void setPathDownload(String pathDownload) {
		ExporToPDF.pathDownload = pathDownload;
	}

	public static String getFileNameDownload() {
		return fileNameDownload;
	}



	public static void setFileNameDownload(String fileNameDownload) {
		ExporToPDF.fileNameDownload = fileNameDownload;
	}



	/**************************************
	 * Method name		: exportPDF
	 * Return type		: void
	 * Description		:Xuat ra file pdf.parentDir_Template:Ten thu muc chua template,
	 * 						fileNameTemplate:file template;parentDir_Download:thum muc download
	 * 						parameter:tham so truye theo; fileNameDisplay:ten file se hien thi
	 * 						listData : danh sach du lieu;
	 * Created date		: Aug 22, 2008
	 * Author			: Honglk
	 **************************************/
	public  void exportToPDF(	String parentDir_Template,
									String fileNameTemplate,
									String parentDir_Download,
									HashMap<String,String> parameter,
									String fileNameDisplay,
									List<T> listData)
	{
//		Connection connect = null;
//		connect = Connect.SINGLETON.getConnection();
		//this.createTemplate(parentDir_Template, fileNameTemplate);
		String jasperfilePath = getPhysicPathReportjaspher(parentDir_Template, fileNameTemplate);
		JasperPrint jasperPrint = new JasperPrint();
		if(parameter == null){
			parameter = new HashMap<String, String>();
		}
		try {
			JRDataSource datasource = this.createDataSource(listData);
			jasperPrint = JasperFillManager.fillReport(jasperfilePath,
			         parameter, datasource);
//			JRPdfExporter exporter = new JRPdfExporter();
//			 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
//			 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,fileNameDisplay+".pdf" );
//			 exporter.exportReport();
			
			//duong dan de save file
			String path_Save = getPhysicPathReport_Save(parentDir_Download, fileNameDisplay);
			//end
			
			//save file pdf xuong
			JasperExportManager.exportReportToPdfFile(jasperPrint,path_Save );
			//end
			
			//duong dan de download
			setPathDownload(getPhysicPathReportDownload(parentDir_Download, getFileNameDownload()));
			//END
		} catch (JRException e) {
			e.printStackTrace();
		}
		
	}
	
	/**************************************
	 * Method name		: createDataSource
	 * Return type		: JRDataSource
	 * Description		:chuan bi nguon du lieu de xuat ra file pdf
	 * Created date		: Aug 22, 2008
	 * Author			: Honglk
	 **************************************/
	private  JRDataSource createDataSource(Collection<T> collection){
		JRDataSource datasource = null;
		datasource = new JRBeanCollectionDataSource(collection);
		return datasource;
	}
	
	/**************************************
	 * Method name		: createTemplate
	 * Return type		: void
	 * Description		:Tao mau template de xuat ra bao cao pdf
	 * Created date		: Aug 22, 2008
	 * Author			: Phonglt
	 **************************************/
	private void createTemplate(String sTenModule,String fileNameTemplate){
		try{
			String pathjrxml = this.getPhysicPathReportsjrxml(sTenModule,fileNameTemplate);
			String pathjaspher = this.getPhysicPathReportjaspher(sTenModule,fileNameTemplate);
			//File file = new File(pathjaspher);
			// neu file jaspher chua ton tai thi thuc hien,de dam bao rang chi tao template 1 lan
			if(!(new File(pathjaspher).exists())){
			JasperCompileManager.compileReportToFile(pathjrxml);
			}
		}catch(JRException e){
			e.printStackTrace();
		}
	}
	
	/**************************************
	 * Method name		: getPhysicPathReportjaspher
	 * Return type		: String
	 * Decription		:Lay duong dan chua file template(jasper)
	 * Created date		: Jul 26, 2008
	 * Author			: Honglk
	 **************************************/
	public  String getPhysicPathReportjaspher(String sTenModule, String sTenFile) {
		String _sPath = "";

		String _sRootPath = UtilsOnScopes.getSession().getServletContext()
				.getRealPath("");

		sTenModule = sTenModule.toLowerCase();
		_sPath = _sRootPath + "\\reports\\" + sTenModule + "\\" + sTenFile+".jasper";
		System.out.println("path:" +_sPath);
		return _sPath;
	}
	/**************************************
	 * Method name		: getPhysicPathReportsjrxml
	 * Return type		: String
	 * Decription		:Lay duong dan chua file template(jrxml)
	 * Created date		: Jul 26, 2008
	 * Author			: Honglk
	 **************************************/
	public  String getPhysicPathReportsjrxml(String sTenModule, String sTenFile) {
		String _sPath = "";
		
		String _sRootPath = UtilsOnScopes.getSession().getServletContext()
		.getRealPath("");
		
		sTenModule = sTenModule.toLowerCase();
		_sPath = _sRootPath + "\\reports\\" + sTenModule + "\\" + sTenFile+".jrxml";
		System.out.println("path:" +_sPath);
		return _sPath;
	}
	
	
	/**************************************
	 * Method name		: getPhysicPathReport_Save
	 * Return type		: String
	 * Decription		:Luu xuong 
	 * Created date		: Jul 26, 2008
	 * Author			: Honglk
	 **************************************/
	public static String getPhysicPathReport_Save(String sTenModule, String tenFile) {
		String _sPath = "";
		String sTenFile = tenFile +".pdf";
		
		String _sRootPath = UtilsOnScopes.getSession().getServletContext()
		.getRealPath("");
		int indexOfStr = sTenFile.indexOf(".");
		String strFirst = sTenFile.substring(0,indexOfStr);
		String strLast = sTenFile.substring(indexOfStr);
		String temp = DateUtils.LayNgayThangHienTaiGio1();
		String sTenFileTemp = strFirst +"-"+temp +strLast;
		
		String sTenFileTempEnd = "";
		StringTokenizer token = new StringTokenizer(sTenFileTemp," ,:");
		sTenFileTempEnd = token.nextToken()+"-"+token.nextToken()+"-"+token.nextToken()
		+"-"+token.nextToken();
		
		setFileNameDownload(sTenFileTempEnd);
		System.out.println(sTenFileTempEnd);
		sTenModule = sTenModule.toLowerCase();
		_sPath = _sRootPath + "\\reports\\" + sTenModule + "\\" + sTenFileTempEnd;
		realpath = _sRootPath + "\\reports\\" + sTenModule;
		System.out.println("path:" +_sPath);
		return _sPath;
	}
	
	/**************************************
	 * Method name		: getPhysicPathReportDownload
	 * Return type		: String
	 * Decription		:Lay duong dan chi den file pdf de donwload ve
	 * Created date		: Jul 26, 2008
	 * Author			: Honglk
	 **************************************/
	public  static String getPhysicPathReportDownload(String sTenModule, String sTenFile) {
		String _sPath = "";
		sTenModule = sTenModule.toLowerCase();
		String _sRootPath = UtilsOnScopes.getSession().getServletContext().getContextPath();
		_sPath = _sRootPath+"/reports/" + sTenModule + "/" + sTenFile ;
		System.out.println("path download:" +_sPath);
		return _sPath;
	} 

}
